package com.samehope.ar.controller;


import com.samehope.ar.common.CommonResult;
import com.samehope.ar.dto.TeamMemberAddParam;
import com.samehope.ar.dto.TeamMemberUpdateParam;
import com.samehope.ar.service.IProjectTeamMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * <p>
 * 项目团队成员 前端控制器
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@RestController
@RequestMapping("/projectTeamMember")
public class ProjectTeamMemberController {

    @Autowired
    private IProjectTeamMemberService teamMemberService;

    /**
     * 添加 项目/团队 成员
     * @param param
     * @return
     */
    @PreAuthorize("hasRole('PROJECT')")
    @PostMapping("/add")
    public CommonResult add(@Valid @RequestBody TeamMemberAddParam param) {
        teamMemberService.add(param);
        return CommonResult.success(null);
    }

    /**
     * 修改 项目/团队 成员关系
     * @param param
     * @return
     */
    @PreAuthorize("hasRole('PROJECT')")
    @PutMapping("/update")
    public CommonResult update(@Valid @RequestBody TeamMemberUpdateParam param) {
        teamMemberService.update(param);
        return CommonResult.success(null);
    }

    /**
     * 从项目中移除该用户
     * @param userId
     * @return
     */
    @PreAuthorize("hasRole('PROJECT')")
    @DeleteMapping("/delete/{userId}")
    public CommonResult delete(@PathVariable("userId") Long userId) {
        teamMemberService.delete(userId);
        return CommonResult.success(null);
    }

    /**
     * 根据团队查询成员信息列表
     * @param teamId
     * @return
     */
    @PreAuthorize("hasAnyRole('PROJECT')")
    @GetMapping("/members/{teamId}")
    public CommonResult members(@PathVariable("teamId") Long teamId) {
        return CommonResult.success(teamMemberService.members(teamId));
    }

    /**
     * 获取团队成员下拉框数据
     * @return
     */
    @PreAuthorize("hasAnyRole('PROJECT')")
    @GetMapping("/list")
    public CommonResult listForSelect() {
        return CommonResult.success(teamMemberService.listForSelect());
    }
}
